package com.power.using.sharlockchat.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jpeng.jptabbar.JPTabBar;
import com.jpeng.jptabbar.OnTabSelectListener;
import com.power.using.sharlockchat.R;
import com.power.using.sharlockchat.eventbus.StartNewFragmentEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportFragment;

/**
 * If you change her, tell me
 * sometimes I believe compiler ignores all my comments
 * Created by gyyak46 on 2017/6/19.
 */
public class MainFragment extends SupportFragment {
    @BindView(R.id.main_tabbar)
    JPTabBar mTabbar;
    Unbinder unbinder;
    private SupportFragment[] mFragments = new SupportFragment[4];
    public static final int FIRST = 0;
    public static final int SECOND = 1;
    public static final int THIRD = 2;
    public static final int FOUR = 3;
    public int prePosition = 0;

    public static MainFragment newInstance() {

        Bundle args = new Bundle();
        MainFragment fragment = new MainFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView(view);
        return view;
    }

    private void initView(View view) {
        EventBus.getDefault().register(this);
        mTabbar.setTitles("问题列表", "已被受理", "导出信息", "个人信息")
                .setNormalIcons(
                        R.drawable.shouyenormal,
                        R.drawable.jilunormal,
                        R.drawable.jiayous,
                        R.drawable.wodenormal)
                .setSelectedIcons(
                        R.drawable.shouyeselected,
                        R.drawable.jiluselected,
                        R.drawable.jiayouses,
                        R.drawable.wodeselected)
                .generate();

        //显示圆点模式的徽章
        // mTabbar.showBadge(1, 50);
        // 点击tab的回调
        mTabbar.setTabListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int index) {
                if (prePosition == -1) {
                    prePosition = 0;
                }
                showHideFragment(mFragments[index], mFragments[prePosition]);
                prePosition = index;
            }

            @Override
            public boolean onInterruptSelect(int index) {
                return false;
            }
        });
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        SupportFragment firstFragment = findChildFragment(HomeFragment.class);
        if (firstFragment == null) {

            mFragments[FIRST] = HomeFragment.newInstance();  //首页
            mFragments[SECOND] = SolvedFrament.newInstance();  //报警
            mFragments[THIRD] = FileOutFrament.newInstance();  //查询
            mFragments[FOUR] = SelfInfoFrament.newInstance();

            loadMultipleRootFragment(R.id.fl_tab_container, FIRST,
                    mFragments[FIRST],
                    mFragments[SECOND],
                    mFragments[THIRD],
                    mFragments[FOUR]);
        } else {
            mFragments[FIRST] = firstFragment;
            mFragments[SECOND] = findChildFragment(SolvedFrament.class);
            mFragments[THIRD] = findChildFragment(FileOutFrament.class);
            mFragments[FOUR] = findChildFragment(SelfInfoFrament.class);
        }

    }


    @Subscribe
    public void startNewFragment(StartNewFragmentEvent event) {
        if (!event.targetFragment.isAdded()) {
            start(event.targetFragment);
        }
    }

    @Override
    public void onDestroyView() {
        EventBus.getDefault().unregister(this);
        super.onDestroyView();
        unbinder.unbind();

    }


}
